/*
Final version : 2008-08-12
*/
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <sstream>
using namespace std;

int mx2csv(char* filename,char* savefilename,int opt,int col)
{
	char tmp[300]={0};
	char *tstr=0;
	fstream fp,fpsave;
	fp.open(filename,fstream::in);
	fpsave.open(savefilename,fstream::app | fstream::out);
	int j=0,icol=1,k=0;
	int meas=0;


	if(fp.fail() || fpsave.fail())
	{
		cout << "error!! file can't read or write.\n";
		return 0;
	}
	while(!fp.eof())
	{
		fp.getline(tmp,300);
		cout << tmp << endl;
		if(tmp[0]=='$')
		{
			if((opt & 2)==0)
			{
				fpsave.write(tmp,300);
                fpsave.put('\n');
			}
			icol=1;
		}
		else if (tmp[0]=='.')
		{
			if((opt & 4)==0)
			{
                fpsave.write(tmp,300);
                fpsave.put('\n');
			}
			icol=1;
		}
		else
		{
			tstr=strtok(tmp," ");
			while(tstr!=NULL)
			{
				meas = 0;
				for(j=0;j<strlen(tstr);j++)
				{
					if(tstr[j] !=' ')
						meas = 1;
					if(tstr[j] ==' ')
						break;
				}

				if((opt & 8) == 0 || k == 1)
				{
					fpsave.write(tstr,j);
					fpsave.put(',');
				}
				tstr=strtok(NULL," ");
				icol++;

			}
			if((opt & 16)==0 && (meas ==1))
			{
				fpsave.put('\n');
			}
			else if(((opt & 16)!=0) && (icol >=col))
			{
				if((opt & 8) == 0 || k == 1 )
					fpsave.put('\n');
				
				icol=1;
				k=1;
			}
				
		}
	}
	fp.close();
	fpsave.close();
	return 1;
}



int main(int argc,char* argv[])
{
	int option=0;
	int colnumber=4;
	int i=3,j=0;
	int batch_start=0;
	int batch_end=0;
	bool is_batch_mode=false;
	char file_name[100];
	//fstream fp;
	stringstream ss;
	
/*
opt =
        N/A or -A       0x01    default
        -C              0x02    remove "$...."
        -T              0x04    remove ". ..."
        -M              0x08    remove measurement name
        -R              0x10    set number of item per row
        -i		0x20	batch processing
		-s				0x20
		-e				0x40
*/
	char opt[]={'A','C','T','M','R','i'};

	if(!(argc>=3))
	{
		cout << "usage:mx2csv filename  save-filename [-option] \n";
		cout << "options:" << endl;
		cout << "	-A		:default" << endl<<  "	-C		:remove \"$....\"" << endl
			<< "	-T		:remove \". ....\"" <<endl<<"	-M		:remove measurement name" << endl
			<< "	-R		:set number of item per row" << endl
			<< "	-i #start #end	:batch processing" << endl;
		cout << "ver. 2008-08-12" << endl;
		return 0;
	}
	else
	{
		for(;i<argc;i++)
		{
			for(j=0;j<6;j++)
			{
				if(argv[i][1]==opt[j])
				{
					option+=(int)pow((double)2,j);
                                	if(j==4) // parameter -R 
                                	{
                                        	i++;
						colnumber=atoi(argv[i]);
                                        	break;
	                                }else if(j==5) // parameter -i
	                                {
	                                	is_batch_mode = true;
	                                	i++;
	                                	batch_start = atoi(argv[i]);
	                                	i++;
	                                	batch_end = atoi(argv[i]);
	                                	if(batch_start>=batch_end)
	                                	{
	                                		cout << "Error, #end must be greater than #start.";
	                                		return 0;
	                                	}
	                                	break;
	                                }
					break;
				}
			}
		}
		
		//cout << option << "," << colnumber << endl;
		if(is_batch_mode)
		{
			for(i=batch_start;i<=batch_end;i++)
			{
				ss.str("");
				ss.clear();
				ss << i;
				cout << ss.str() << endl;
				strcpy(file_name,argv[1]);
				strcat(file_name,ss.str().c_str());
				mx2csv(file_name,argv[2],option,colnumber);
				cout << argv[1] << i << "'s data has added to " << argv[2] <<endl;
			}
		}
		else
		{
			mx2csv(argv[1],argv[2],option,colnumber);
			cout << argv[1] << "'s data has added to " << argv[2] <<endl;
		}
	}
}
